//性别单选框
(function(){
    $('a.radiosex').click(function(){
        $(this).addClass('on').siblings().removeClass('on');
    });
    $('a.edit').click(function(){
        $(this).siblings('input').attr('readonly',false).focus();
    });
    $(".group input[type='text']").blur(function(){
        $(this).attr('readonly',true);
    });
})();
$('a.admin-userinfo-edit').click(function(){
    $(this).siblings('input').attr('readonly',false).focus();
});


//签到
(function(){
    var qd_time = new Date();
    var qd_year =qd_time.getFullYear();
    var qd_month =qd_time.getMonth()+1<10? '0'+(qd_time.getMonth()+1):(qd_time.getMonth()+1);
    var qd_d =qd_time.getDate();
    var qd_day = qd_time.getDate()<10?'0'+qd_time.getDate():qd_time.getDate();
    var qd_days;
    $.ajax({
        url:'http://120.79.73.159/api/home/sign_famous',
        type:'POST',
        datatype:'json',
        data:{
            uid:$.cookie('user_id'),
            token:$.cookie('user_token')
        },success:(function(ea){
            ea = JSON.parse(ea);
            if(ea.ret==200){
                days = ea.data.data;
                function GetDateStr(AddDayCount) {
                    var dd = new Date();
                    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
                    var y = dd.getFullYear();
                    var m = dd.getMonth()+1<10?'0'+(dd.getMonth()+1):dd.getMonth()+1;
                    var d = dd.getDate()<10?'0'+dd.getDate():dd.getDate();
                    return y+"-"+m+"-"+d;
                }
                daylist =  "<li data-qd='"+GetDateStr(-3)+"'>"+ GetDateStr(-3).substring(8,10) +"</li>" +
                    "<li data-qd='"+GetDateStr(-2)+"'>"+GetDateStr(-2).substring(8,10)+"</li>" +
                    "<li data-qd='"+GetDateStr(-1)+"'>"+GetDateStr(-1).substring(8,10)+"</li>" +
                    "<li data-qd='"+GetDateStr(0)+"'>"+GetDateStr(0).substring(8,10)+"</li>" +
                    "<li data-qd='"+GetDateStr(1)+"'>"+GetDateStr(1).substring(8,10)+"</li>" +
                    "<li data-qd='"+GetDateStr(2)+"'>"+GetDateStr(2).substring(8,10)+"</li>" +
                    "<li data-qd='"+GetDateStr(3)+"'>"+GetDateStr(3).substring(8,10)+"</li>";
                var qdhtml='';
                ea.data.is_sign ==0 ? qdhtml = '<div id=\'qiandao\' class="qiandao-success qdbtn">点击签到</div>' : qdhtml = '<div id=\'qiandao\' class="qiandao-success">已签到</div>';
                $('.qd_days').html(ea.data.days);
                var fixed =
                    "<div class=\"warning qiandao\">" +
                    "<div class=\"qiandaobox\">" +
                    "<a href=\"javascript:;\" class=\"qiandao-close\">" +
                    "<img src=\"../images/icon_closed.png\" alt=\"\"></a>" +
                    "<div class=\"qiandaobg-box\"><div class=\"qiandao-month\">"+qd_month+"月</div>" +qdhtml +
                    "<div class=\"qiandao-num\">已连续签到 <span class='qd_days'>"+ea.data.days+"</span>天</div>" +
                    "</div><div class=\"qiandao-data-box\">" +
                    "<div class=\"qiandao-data-line-box\">" +
                    "<div class=\"qiandao-data-line\"></div>" +
                    "<div class=\"qiandao-data\">" +daylist+
                    "</div></div></div></div></div><div class=\"warning no_login\"><div class=\"warningbox\"><div class=\"waring-img\"><img src=\"../images/img_tk_logo.png\" alt=\"\"></div><div class=\"warning-text\">您未获得权限，点击申请授权！</div><div class=\"warning-btn\"><a href=\"reg/reg.html\">申请授权</a></div></div></div>";
                $('body').prepend(fixed);
                for(num in days){
                    for(var i =num;i<7;i++){
                        days[num]==$('.qiandao-data li').eq(i).data('qd') ? $('.qiandao-data li').eq(i).addClass('on'): console.log() ;
                    }
                }
            }else{
                alert('您已退出或已在其他设备登陆，请重新登陆')
                $.cookie("user_token",null,{path:'/'});
                $.cookie("user_head",null,{path:'/'});
                $.cookie("user_id",null,{path:'/'});
                $.cookie("user_nickname",null,{path:'/'});
                $.cookie("user_r_token",null,{path:'/'});
                $.cookie("user_sign",null,{path:'/'});
                $.cookie("old_room_id",null,{path:'/'});
                $.cookie("qdm",null,{path:'/'});
                $.cookie("qtoken",null,{path:'/'});
                $.cookie("room_id",null,{path:'/'});
                $.cookie("user_sign",null,{path:'/'});
                $.cookie("room_keyword",null,{path:'/'});
                $.cookie("other_login",null,{path:'/'});
                $(window).attr('location','../reg/login.html');
            }

        })
    });
    $('body').on('click','#qiandao',function(){
        $.ajax({
            url: 'http://120.79.73.159/api/home/sign_in',
            type: 'POST',
            datatype: 'json',
            data: {
                uid:$.cookie('user_id'),
                token:$.cookie('user_token')
            },success:(function(qd){
            })
        });
    });
    $(document).on('click','.qiandao-close',function () {
        $('.warning.qiandao').hide();
    });
    $('.success .qiandao').click(function () {
        $('.warning.qiandao').show();
    });
})();

(function(){
    $('.warningbox').click(function () {
        event.stopPropagation?event.stopPropagation():window.event.cancelBubble = true;
    });
    $('.newroombox').click(function () {
        event.stopPropagation?event.stopPropagation():window.event.cancelBubble = true;
    });
    $('.qiandaobox a.qiandao-close').click(function () {
        $('.warning.qiandao').hide();
    });
    $('.success .qiandao').click(function () {
        $('.warning.qiandao').show();
    })
    // $('.warning').click(function(){
    //     $('.warning').hide();
    // });
})();


//数据初始化
$(window).ready(function(){
    if( typeof $.cookie('user_token')=='undefined'||$.cookie('user_token')=='null'){
        alert('请先登陆')
        $(window).attr('location','../reg/login.html')
    }
    $('.top-tips span').html($.cookie('user_nickname')+'，您好！')
    $.ajax({
        type:'POST',
        datatype:'json',
        url:'http://120.79.73.159/api/personal/get_information',
        data:{uid:$.cookie('user_id'),token:$.cookie('user_token')},
        success:(function(userinfo){
            var info = JSON.parse(userinfo);
            $('.top_username').html(info.data.nickname);
            $('.cen_username').html(info.data.nickname);
            $('.user-left-head').attr('src',info.data.head);
            $('.user_id').html("ID:"+info.data.id);
            $('.user_hot').html(info.data.hot);
            $('.user_money').html(info.data.money);
            $('.user_tel_number').val(info.data.mobile);
            $('.user_username').val(info.data.nickname);
            $('.wdzl-from a.radiosex').eq(info.data.sex).addClass('on').siblings().removeClass('on');
            $('.qianming').val(info.data.sign);
            $('.userpicbox img.user-head').attr('src',info.data.head);
            $("#eprovinceName").val(info.data.province);
            $("#eprovinceName").trigger("change");
            $("#ecityName").val(info.data.city);
            $("#ecityName").trigger("change");
            $("#edistrictName").val(info.data.district);
        })
    });
});

$(document).on('click','.result-btn',function(){
    $.cookie('old_room_id',$.cookie('room_id'),{path:'/'});
    $.cookie('room_id',$(this).data('roomid'),{path:'/'});
    $(window).attr('location','../chatroom.html');
});